package GUI;


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.CharArrayWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.swing.JTable;
import javax.swing.JScrollPane;

import client.ClientConsole;

import Library.Histogram;
import Library.LateReport;
/**
 * This class display report for late loans 
 */
public class GUI_LateReport extends JPanel{
	/**
	 * client Connected
	 */
	private ClientConsole clc;
	/**
	 *  label of the average
	 */
	private JLabel lblavg;
	/**
	 * label of the result average
	 */
	private JLabel lblgetavg;
	/**
	 *  label of the median
	 */
	private JLabel lblmed;
	/**
	 *  label of the BookId
	 */
	private JLabel lblBookId;
	/**
	 * label of the result median
	 */
	private JLabel lblgetmed;
	/**
	 * Button to get report
	 */
	private JButton btnGetReport;
	/**
	 * list to number of  the late loans
	 */
	private LateReport lateReport;
	/**
	 * panel Histogram
	 */
	private Histogram h;
	/**
	 * Text field to the bookId
	 */
	private JTextField bidTextField;
	/**
	 * bookId in string
	 */
	private String bid = null;	
	/**
	 * panel for late report
	 */
	private GUI_LateReport lr = this;
	/**
	 * Display statistic info to late loans 
	 * @param cc
	 * @param gl
	 * @param r
	 * @param lReport
	 */
	public GUI_LateReport(ClientConsole cc, final GUI_LogIn gl,final GUI_Report r, LateReport lReport)
	{
		clc=cc;
		this.setSize(700,600);
		setLayout(null);
		

		this.lateReport = lReport;
		JLabel lblLateInLoan = new JLabel("Late In Loan Report");
		lblLateInLoan.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblLateInLoan.setBounds(275, 11, 181, 26);
		add(lblLateInLoan);

		lblavg = new JLabel("Average: ");
		lblavg.setBounds(22, 96, 136, 23);
		add(lblavg);
		
		lblgetavg = new JLabel(lateReport.getAvg().toString());
		lblgetavg.setBounds(122, 96, 136, 23);
		add(lblgetavg);
		
		lblmed = new JLabel("Median: ");
		lblmed.setBounds(22, 120, 136, 23);
		add(lblmed);
		
		lblgetmed = new JLabel(lateReport.getMed().toString());
		lblgetmed.setBounds(122, 120, 136, 23);
		add(lblgetmed);
		
		lblBookId = new JLabel("BookId: ");
		lblBookId.setBounds(22, 300, 136, 23);
		add(lblBookId);
		
		JButton btnHistogram = new JButton(" show histogram ");
		btnHistogram.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				ArrayList<Long> list=lateReport.lateReportList;
				if(list.size()>0)
				{
				Long sizeArr=(list.get(list.size()-1));
				int sizeArr1=Integer.parseInt(sizeArr.toString());
				String [] s =new String[sizeArr1+1] ;
				
				for (Integer i=0;i<sizeArr1+1;i++)
					s[i]=i.toString();
				
				int [] val =new int[sizeArr1+1];
				
				for (Integer i=0;i<list.size();i++)
				{
					int v =Integer.parseInt(list.get(i).toString());
					val[v]++;
				}
               
               
                for (Integer i=0;i<sizeArr1+1;i++)
				{
                	val[i]=val[i]*10;
				}
                int max=val[0];
                for (Integer i=0;i<sizeArr1+1;i++)
				{
					if(max<val[i])
						max=val[i];
				}
				h=new Histogram(s,val,max/5,100);
				int flg = h.his();
				if (flg==0)
					JOptionPane.showMessageDialog(new JFrame(),"Not enough data");
				}
				else
					JOptionPane.showMessageDialog(new JFrame(),"No values,can not be displayed Histogrma");
			}
		});
		btnHistogram.setBounds(22, 152, 166, 19);
		add(btnHistogram);
	
		bidTextField = new JTextField();
		bidTextField.setBounds(183, 300, 100, 20);
		add(bidTextField);
		bidTextField.setColumns(10);
		
		
		
		btnGetReport = new JButton("Get Report");
		btnGetReport.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				bid = bidTextField.getText();
				if (bid.equals(null))
					JOptionPane.showMessageDialog(new JFrame(),"Please enter book ID");
				else
				{
					String s[]={"lateReportBook",bid};
					try {			
						clc.client.sendToServer(s);
						while(clc.client.flag==0)
						    System.out.print("");
						    clc.client.flag=0;
						    LateReport lateReport =(LateReport)clc.client.message;
						    if (lateReport.getFlg()==0)
								JOptionPane.showMessageDialog(new JFrame(),"There are no data to display");
						    else
						    {
						    	GUI_LateReportBook lrb=new GUI_LateReportBook(clc,gl,lr,lateReport);
								gl.setContentPane(lrb);
						    }
					} catch (IOException e1) {
						e1.printStackTrace();
					}	// send message to server
			
				}
			}
		});
		btnGetReport.setBounds(183, 340, 100, 20);
		add(btnGetReport);
		
		JButton btnBack = new JButton("Back");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				gl.setContentPane(r);
			}
		});
		btnBack.setBounds(401, 441, 89, 23);
		add(btnBack);
		
		JButton btnLogOut = new JButton("Log Out");
		btnLogOut.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				gl.LogOut();
			}
		});
		btnLogOut.setBounds(500, 441, 89, 23);
		add(btnLogOut);
	}
	
	
}
